﻿using System;
using LF.Domain.Entities;
using LF.Domain.Services;
using LF.Shared.Dtos.Input;
using LF.Shared.Dtos.Out;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.ObjectMapping;
using Volo.Abp.Uow;

namespace LF.Application.Services.Iml
{
    [Authorize]
    [ApiExplorerSettings(GroupName = SwaggerGroup.Sys)]
    public class AppinfoService : BaseService, IAppinfoService
    {
        IAppManagerService _service;
        public AppinfoService(IObjectMapper<LFModule> mapper,
            IAppManagerService service,
            IGlobalManagerService globalManager) : base(mapper, globalManager)
        {
            _service = service;
        }
        [UnitOfWork]
        public virtual Result<RuleOut> ConfigRule(RuleInput input)
        {
            var old = _mapper.Map<RuleInput, Rule>(input);
            var rule = _service.InsertOrUpdate(old);
          
            var result = _mapper.Map<Rule, RuleOut>(rule);
            return Ok(result);
        }

     

        public Result<AppDicOut[]> GetDic()
        {
            var result = _service.GetAppDic();
            var list = _mapper.Map<AppDic[], AppDicOut[]>(result);
            return Ok(list);
        }

        public Result<RuleOut> GetRule()
        {
            var rule = _service.GetRule();
            var result = _mapper.Map<Rule, RuleOut>(rule);
            return Ok(result);
        }

        

        public Result<NoticeListOut> Notices(NoticeSearch seach)
        {
            var result = _service.GetNotices(seach.Index, seach.PageSize, seach.Key);
            var count = _service.GetNoticeCount();
            var list = _mapper.Map<List<Notice>, List<NoticeOut>>(result);
            var listOut = new NoticeListOut
            {
                TotalCount = count,
                Items = list
            };
            return Ok(listOut);
        }

        public Result Notice(NoticeOut input)
        {
            var notice = _mapper.Map<NoticeOut, Notice>(input);
            if (input.Id == null)
            {
                _service.InsertNotice(notice);
            }
            else
            {
                _service.UpdateNotice(notice);
            }
            return Ok();
        }
        [AllowAnonymous]
        public Result<NoticeListOut> WxNotices(NoticeWxSearch seach)
        {
            var result = _service.GetNotices(seach.Index, seach.PageSize, seach.Date);
            var count = _service.GetNoticeCount();
            var list = _mapper.Map<List<Notice>, List<NoticeOut>>(result);
            var listOut = new NoticeListOut
            {
                TotalCount = count,
                Items = list
            };
            return Ok(listOut);
        }

        public Result<AppDicOut[]> GetDicGroup(string group)
        {
            var result = _service.GetAppDic(group);
            var list = _mapper.Map<AppDic[], AppDicOut[]>(result);
            return Ok(list);
        }
    }
}

